// pages/goods_detail/index.js
import {
  getGoodsDetailPort
} from '../../http/http'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    iid: '',
    goodsBerList: {},
    arr: [{}]
  },
  goodsBerList: {},
  goShopFn() {
    wx.switchTab({
      url: '/pages/cart/index',
    })
  },
  addshoppFn() { //加入购物车

    let shopping = wx.getStorageSync("shopping") || [];
    let index = shopping.findIndex(v => v.goods_id === this.goodsBerList.goods_id);
    console.log(index);
    if (index === -1) {
      this.goodsBerList.num = 1;
      this.goodsBerList.checked = true;
      shopping.push(this.goodsBerList);
    } else {
      shopping[index].num++;
    }

    wx.setStorageSync("shopping", shopping);
    wx.showToast({
      title: '加入成功',
      icon: 'success',
      mask: true
    });

  },
  purchaseFn() { //立即购买

  },
  async getgoodsdetailFn() {
    let res = await getGoodsDetailPort(this.data.iid)
    console.log(res);
    this.goodsBerList = res.data.message
    this.setData({
      goodsBerList: res.data.message
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    this.setData({
      iid: options.iid
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getgoodsdetailFn()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})